package algotradingfx.data;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;

import atp.commons.util.Tick;

public class TickWriter extends BufferedWriter {
	private static final String TAB = "\t";
	private static final DecimalFormat decimalFormat = new DecimalFormat("0.00000");
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd:HH:mm:ss:SSS");

	public String toString(Tick t) {
		StringBuilder builder = new StringBuilder();
		builder.append(t.getTimestamp());
		builder.append(TAB);
		builder.append(dateFormat.format(t.getTimestamp()));
		builder.append(TAB);
		builder.append(t.getSymbol());
		builder.append(TAB);
		builder.append(decimalFormat.format(t.getBid()));
		builder.append(TAB);
		builder.append(t.getBidDepth());
		builder.append(TAB);
		builder.append(decimalFormat.format(t.getAsk()));
		builder.append(TAB);
		builder.append(t.getAskDepth());
		builder.append(TAB);
		builder.append(decimalFormat.format(t.getSpread()));
		return builder.toString();
	}

	public TickWriter(Writer writer) {
		super(writer);
		decimalFormat
				.setDecimalFormatSymbols(new DecimalFormatSymbols(
						Locale.ENGLISH));
	}

	public void writeTick(Tick t) throws IOException {
		write(toString(t));
		newLine();
		flush();
	}
}
